<?php
// 导入基础类
require_once(dirname(__FILE__).'/base.class.php');
class Category extends Base
{
    function __construct()
    {
        // 调用父类的构造函数
        parent::__construct();
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    // 获取课程分类列表
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        $sql = "select * from course_classify";
        $sql .= " limit {$start},{$page_num}";
        
        $list = $this->db->query($sql);
        if (!empty($list)) {
            // 将路径处理成绝对路径
            foreach($list as $key=>$item){
                $list[$key]['icon']=path($item['icon']);
            }
            // 获取总记录数
            $sql = "select count(id) as count from course_classify";
            $res = $this->db->get_one($sql);
            $output['data'] =['list'=> $list,'total'=> $res['count']];
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取课程分类失败';
        }
        echo json_encode($output);
    }

    /*
        获取分类课程
     */ 
    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $sql = "select * from course_classify where id={$id}";
            $res = $this->db->get_one($sql);
            if ($res) {
                $res['icon']=path($res['icon']);
                $output['data'] = $res;
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }

}
